package com.fenghongzhang.kuaishou.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.fenghongzhang.kuaishou.R;

import java.util.ArrayList;

/**
 * 要求
 * 1,可以设备背景色
 * 2,可以设置默认和选中文字颜色
 * 3,可以设置图片和选中的图片
 * 4,可以设置文字大小和选中的大小
 *
 */
public class MyBottomNavigationBar extends FrameLayout implements View.OnClickListener{


    //背景色
    private int backgroudColor;

    //盛放bar的容器
    private ArrayList<MyBottomBarItem> barItemsList;
    //排布bar的布局
    LinearLayout barContainer;
    private LinearLayout.LayoutParams params;
    //选中的颜色
    int activityColor;
    //选中的图片
    int activityImg;

    public MyBottomNavigationBar(@NonNull Context context) {
        super(context);
        init(context,null);
    }

    public MyBottomNavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs){

        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        barItemsList = new ArrayList<>();

        backgroudColor = Color.BLACK;
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.my_navigation_bar_layout, this, true);
        FrameLayout viewById = inflate.findViewById(R.id.my_bottom_navigation_container);
        viewById.setBackgroundColor(Color.parseColor("#161714"));
        barContainer = inflate.findViewById(R.id.my_bar_container);
        params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        params.weight = 1;
        barContainer.setGravity(Gravity.CENTER);

    }

    public MyBottomNavigationBar setBackgroudColor(int backgroudColor) {
        this.backgroudColor = backgroudColor;
        return this;
    }

    public MyBottomNavigationBar addItem(MyBottomBarItem item){
        barItemsList.add(item);
        return this;
    }


    public void build(){
        for (int i = 0; i < barItemsList.size(); i++) {
            MyBottomBarItem myBottomBarItem = barItemsList.get(i);
            if(myBottomBarItem.getTitle() != null){
                TextView textView = new TextView(getContext());
                textView.setText(myBottomBarItem.getTitle());
                textView.setOnClickListener(this);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(params);
                //第一个选中状态
                if(i == 0){
                    textView.setTextSize(18);
                    textView.setTypeface(Typeface.DEFAULT_BOLD);
                    textView.setTextColor(getResources().getColor(R.color.selectedTextColor));
                }else{
                    textView.setTextSize(16);
                    textView.setTypeface(Typeface.DEFAULT);
                    textView.setTextColor(getResources().getColor(R.color.unselectedTextColor));
                }
                textView.setTextColor(getResources().getColor(R.color.unselectedTextColor));
                textView.setTag(R.id.bottom_bar_position_key,i);
                barContainer.addView(textView);
            }
            if(myBottomBarItem.getDrawable() != null){
                ImageView imageView = new ImageView(getContext());
                imageView.setImageDrawable(myBottomBarItem.getDrawable());
                imageView.setTag(R.id.bottom_bar_position_key,i);
                imageView.setOnClickListener(this);
                imageView.setLayoutParams(params);
                barContainer.addView(imageView);
            }
        }
    }


    //设置第一个默认选中的下标
    public void setPosition(int position){
        onItemSelectedListener.onTabSelected(position);
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag(R.id.bottom_bar_position_key);
        onItemSelectedListener.onTabSelected(tag);
    }

    private OnItemSelectedListener onItemSelectedListener;
    public interface OnItemSelectedListener {
        void onTabSelected(int position);
        void onTabUnselected(int position);
    }

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public void selectTextStyle(int position){
        for (int i = 0; i < barItemsList.size(); i++) {
            View childAt = barContainer.getChildAt(i);
            if(i == position){
                if(childAt instanceof TextView){
                    ((TextView) childAt).setTextSize(18);
                    ((TextView) childAt).setTypeface(Typeface.DEFAULT_BOLD);
                    ((TextView) childAt).setTextColor(getResources().getColor(R.color.selectedTextColor));
                }
            }else{
                if(childAt instanceof ImageView){

                }else{
                    ((TextView) childAt).setTextSize(16);
                    ((TextView) childAt).setTypeface(Typeface.DEFAULT);
                    ((TextView) childAt).setTextColor(getResources().getColor(R.color.unselectedTextColor));
                }
            }

        }
    }
    public void unselectTextStyle(){

    }
}
